#qprinter howto
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView,QComboBox,QTextBrowser
from PyQt5.QtGui import QStandardItemModel,QStandardItem,QPainter
from PyQt5.QtCore import Qt
from PyQt5.QtPrintSupport import QPrinter, QPrintDialog
class MyWin(QMainWindow):
    def clip(self):
        print("print")
        self.printer = QPrinter(QPrinter.HighResolution)
        dialog = QPrintDialog(self.printer)
        if dialog.exec_():
            self.painter = QPainter(self.printer)
            self.main.render(self.painter)    
            self.painter.end()
    def __init__(self):
        super().__init__()
        action_clip = QtWidgets.QAction(self)
        action_clip.setObjectName("action_clip")
        action_clip.setText("print")
        action_clip.triggered.connect(self.clip)
        tb=self.addToolBar('File')
        tb.addAction(action_clip)
        self.main = QTextBrowser(minimumWidth=800, minimumHeight=600)

        # Must come before the HTML is inserted
        self.main.document().setDefaultStyleSheet(
            'body {color: #333; font-size: 14px;} '
            'h2 {background: #CCF; color: #443;} '
            'h1 {background: #001133; color: white;} '
        )

        # TextBrowser background is a widget style, not a document style
        self.main.setStyleSheet('background-color: #EEF;')
        with open('example.html', 'r') as fh:
            self.main.insertHtml(fh.read())

        self.main.setOpenExternalLinks(True)
        self.setCentralWidget(self.main)


def set_up_table_view():
    # 创建应用程序对象
    app = QApplication(sys.argv)

    # 创建主窗口对象
    main_window = MyWin()
    main_window.resize(600,600)
    main_window.show()
    sys.exit(app.exec_())
set_up_table_view()
